var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const cors = require('cors');

// 导入路由模块 ---相当于接口API
var indexRouter = require('./routes/web/index');
var stashRouter = require('./routes/api/stash');
// var repairRouter = require('./routes/api/repair');
var businessRouter = require('./routes/api/business');
var usersRouter = require('./routes/api/users');

//导入路由模块---newAPi接口   --文件名称和路由的名称不能一致
const machineRoutes = require('./routes/api/newapi/machineRoutes');//修改机器信息的文档
const statusChangeLogRoutes = require('./routes/api/newapi/statusChangeLogRoutes');//修改机器状态的文档
const businessNewRoutes = require('./routes/api/newapi/businessRoutes');//业务信息文档
const repairNewRoutes = require('./routes/api/newapi/repairRoutes');
const repairNewStartRoutes = require('./routes/api/newapi/repairProgressSchema');
const todoNewRouter = require('./routes/api/newapi/todoRoutes');
const accessoryRequestSchemas = require('./routes/api/newapi/accessoryRequestSchema');


var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(cors()); //跨域

// express注册挂载
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// 注册使用路由文件 --- api接口文件
app.use('/', indexRouter);
app.use('/stash', stashRouter);
// app.use('/repair', repairRouter);
app.use('/business', businessRouter);
app.use('/users', usersRouter);

//注册newapi
app.use('/api', machineRoutes);  //机器信息文档
app.use('/api', statusChangeLogRoutes);  //修改机器状态的文档
app.use('/api', businessNewRoutes);//业务信息文档
app.use('/api', repairNewRoutes);  //修复机器的文档
app.use('/api', repairNewStartRoutes);
app.use('/api', todoNewRouter); // 待办事项
app.use('/api', accessoryRequestSchemas); // 申请配件表


// catch 404 and forward to error handler   
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
